
define(['ionic', 'controllers'], function () {
    angular.module('U9Mobile.controllers')
        .controller('AppCtrl', ['$scope', '$ionicPopover', '$state', 'AppManager', 'UserService', 'UtilService', 'ionicU9ActionSheet',
            function ($scope, $ionicPopover, $state, AppManager, UserService, UtilService, ionicU9ActionSheet) {
                $scope.appData = {
                    orgList: [],
                    selected: '',
                    current: UserService.getOrg(),
                    updateAvailable: false,
                    user: {}
                };

                $ionicPopover.fromTemplateUrl('tpls/more.html', {
                    scope: $scope
                }).then(function (popover) {
                    $scope.popover = popover;
                });
                $scope.$on('$destroy', function() {
                    $scope.popover.remove();
                });

                $scope.pickOrg = function () {
                    $scope.popover.hide().then(function () {
                        if ($scope.appData.orgList.length === 0) {
                            UtilService.confirm('请先登录').then(function (res) {
                                if (res) {
                                    UtilService.goState('app.login');
                                }
                            });
                            return;
                        }
                        $scope.appData.selected = $scope.appData.current.code;
                        ionicU9ActionSheet.show({
                            scope: $scope,
                            template:
                            '<scroll-selector list="appData.orgList" selected="appData.selected" display-field="name" value-field="code" delegate-handle="orgList">' +
                            '</scroll-selector>',
                            buttonClicked: function (index) {
                                if (index) {
                                    changeOrg();
                                }
                                return true;
                            }
                        });
                    });
                };
                $scope.goChat = function () {
                    $scope.popover.hide().then(function () {
                        var logined = UserService.getUserToken().logined;
                        if (!logined) {
                            UtilService.confirm('请先登录').then(function (res) {
                                if (res) {
                                    UtilService.goState('app.login');
                                }
                            });
                            return;
                        }
                        $state.go('app.chat', {
                            fromApp: false
                        });
                    });
                };
                $scope.goReList = function () {
                    $scope.popover.hide().then(function () {
                        $state.go('app.relist');
                    });
                };
                $scope.goUser = function () {
                    var logined = UserService.getUserToken().logined;
                    if (logined) {
                        UtilService.goState('app.user');
                    } else {
                        UtilService.goState('app.login');
                    }
                };
                $scope.goSetting = function () {
                    UtilService.goState('app.setting');
                };
                $scope.logout = function () {
                    UserService.logout();
                    UtilService.goState('app.login');
                };

                $scope.$on('login', function () {
                    refreshAccount();
                });
                $scope.$on('logout', function () {
                    refreshAccount();
                });

                UserService.loginPromise.then(function () {
                    refreshAccount();
                    var userAccount = UserService.getUserParams('account');
                    if (userAccount) {
                        UserService.getOrgList(userAccount).then(function (organizations) {
                            if (organizations && organizations.length > 0) {
                                $scope.appData.orgList = organizations;
                                $scope.appData.current = UserService.getOrg();
                            }
                        });
                    }
                });

                AppManager.loadingAppPromise.then(function () {
                    var installApps = AppManager.getInstallApps();
                    AppManager.getAllApps().then(function (allApps) {
                        $scope.appData.updateAvailable = AppManager.checkUpdateAvailable(installApps, allApps);
                    });
                });

                function refreshAccount () {
                    var logined = UserService.getUserToken().logined;
                    if (logined) {
                        var detail = UserService.getUserDetailInfo();
                        $scope.appData.user.name = detail.name;
                        $scope.appData.user.photo = detail.photo;
                    } else {
                        $scope.appData.user.name = null;
                        $scope.appData.user.photo = null;
                    }
                }
                function changeOrg () {
                    var len = $scope.appData.orgList.length;
                    for (var i = 0; i < len; i++) {
                        if ($scope.appData.orgList[i].code === $scope.appData.selected) {
                            $scope.appData.current = $scope.appData.orgList[i];
                            UserService.setOrg($scope.appData.current);
                        }
                    }
                }
            }
        ]);
});